<script setup>
import { ref, computed, onMounted } from "vue";
import LocalVueUiSparkHistogram from '../src/components/vue-ui-sparkhistogram.vue';
import LocalVueDataUi from '../src/components/vue-data-ui.vue';
import Box from "./Box.vue";
import convertArrayToObject from "./convertModel";
import { VueUiSparkHistogram } from "vue-data-ui"
import { VueUiSparkHistogram as VueUiSparkHistogramTreeshaken } from "vue-data-ui/vue-ui-sparkhistogram";
import ConfigKnobs from "./ConfigKnobs.vue";
import { useConfigurationControls } from "./createConfigModel";
import { useConfig } from "../src/useConfig"

const { vue_ui_sparkhistogram: DEFAULT_CONFIG } = useConfig();

const {
    CHECKBOX,
    NUMBER,
    RANGE,
    TEXT,
    COLOR,
    SELECT,
    createModel
} = useConfigurationControls(DEFAULT_CONFIG);

const dataset = ref(undefined);

onMounted(() => {
    setTimeout(() => {
        dataset.value = [
    {
        value: -1.2,
        valueLabel: "20.35%",
        timeLabel: "09:00",
        intensity: 1,
        color: '#FF0000'
    },
    {
        value: -1.3,
        valueLabel: "50%",
        timeLabel: "10:00",
        intensity: 0.5,

    },
    {
        value: 1.1,
        valueLabel: "60%",
        timeLabel: "11:00",
        intensity: 0.6,
        color: '#00FF00'

    },
    {
        value: 0.8,
        valueLabel: "70%",
        timeLabel: "12:00",
        intensity: 0.7,

    },
    {
        value: 2,
        valueLabel: "100%",
        timeLabel: "13:00",
        intensity: 1,

    },
    {
        value: 2.1,
        valueLabel: "100%",
        timeLabel: "14:00",
        intensity: 1,

    },
    {
        value: 2.3,
        valueLabel: "80%",
        timeLabel: "15:00",
        intensity: 0.8,

    },
    {
        value: 2.1,
        valueLabel: "70%",
        timeLabel: "16:00",
        intensity: 0.7,

    },
    {
        value: 0.9,
        valueLabel: "60%",
        timeLabel: "17:00",
        intensity: 0.6,

    },
    {
        value: 0.7,
        valueLabel: "50%",
        timeLabel: "18:00",
        intensity: 0.5,

    },
    {
        value: 0.3,
        valueLabel: "30%",
        timeLabel: "19:00",
        intensity: 0.3,

    },
    {
        value: 0.2,
        valueLabel: "20%",
        timeLabel: "20:00",
        intensity: 0.2,

    },
]
    }, 2000);
})

const alternateDataset = ref([
{
        value: 2.3,
        valueLabel: "80%",
        timeLabel: "15:00",
        intensity: 0.8,

    },
    {
        value: 2.1,
        valueLabel: "70%",
        timeLabel: "16:00",
        intensity: 0.7,

    },
    {
        value: 0.9,
        valueLabel: "60%",
        timeLabel: "17:00",
        intensity: 0.6,

    },
    {
        value: 0.7,
        valueLabel: "50%",
        timeLabel: "18:00",
        intensity: 0.5,
    },
])

const alternateConfig = ref({
    style: {
        backgroundColor: '#CCCCCC',
            title: {
                text: 'Alternate'
            }
    }
})

const isPropsToggled = ref(false);
function toggleProps() {
    isPropsToggled.value = !isPropsToggled.value;
}

function alterDataset() {
    dataset.value.push({
        value: Math.random(),
        valueLabel: `${Math.round(Math.random() * 100)}%`,
        timeLabel: "ALT",
        intensity: Math.random(),
    },)
}


const model = ref([
    { key: 'debug', def: true, type: 'checkbox'},
    { key: 'loading', def: false, type: 'checkbox'},
    { key: 'style.backgroundColor', def: '#FFFFFF', type: 'color'},
    { key: 'style.fontFamily', def: 'inherit', type: 'text'},
    { key: 'style.animation.show', def: true, type: 'checkbox'},
    { key: 'style.animation.speedMs', def: 500, type: 'range', min: 100, max: 1000},
    { key: 'style.layout.height', def: 96, type: 'number', min: 30, max: 300},
    { key: 'style.layout.width', def: 640, type: 'number', min: 200, max: 1000},

    { key: 'style.layout.padding.top', def: 0, type: 'number', min: 0, max: 48},
    { key: 'style.layout.padding.right', def: 0, type: 'number', min: 0, max: 48},
    { key: 'style.layout.padding.left', def: 0, type: 'number', min: 0, max: 48},
    { key: 'style.layout.padding.bottom', def: 0, type: 'number', min: 0, max: 48},

    { key: 'style.bars.shape', def: 'square', type: 'select', options: ['circle', 'triangle', 'square', 'diamond', 'pentagon', 'hexagon', 'star']},
    { key: 'style.bars.strokeWidth', def: 0, type: 'number', min: 0, max: 12},
    { key: 'style.bars.colors.positive', def: '#3366CC', type: 'color'},
    { key: 'style.bars.colors.negative', def: '#CC6633', type: 'color'},
    { key: 'style.bars.colors.gradient.show', def: true, type: 'checkbox'},
    { key: 'style.bars.borderRadius', def: 24, type: 'range', min: 0, max: 48},
    { key: 'style.bars.gap', def: 12, type: 'range', min: 0, max: 48},

    { key: 'style.labels.value.show', def: true, type: 'checkbox'},
    { key: 'style.labels.value.fontSize', def: 14, type: 'number', min: 8, max: 48},
    { key: 'style.labels.value.color', def: '#1A1A1A', type: 'color'},
    { key: 'style.labels.value.bold', def: true, type: 'checkbox'},
    { key: 'style.labels.value.rounding', def: 1, type: 'number', min: 0, max: 12},
    { key: 'style.labels.value.prefix', def: 'P', type: 'text'},
    { key: 'style.labels.value.suffix', def: 'S', type: 'text'},
    { key: 'style.labels.value.offsetY', def: 0, type: 'number', min: -100, max: 100},

    { key: 'style.labels.valueLabel.show', def: true, type: 'checkbox'},
    { key: 'style.labels.valueLabel.fontSize', def: 14, type: 'number', min: 8, max: 48},
    { key: 'style.labels.valueLabel.color', def: '#1A1A1A', type: 'color'},
    { key: 'style.labels.valueLabel.bold', def: false, type: 'checkbox'},
    { key: 'style.labels.valueLabel.rounding', def: 1, type: 'number', min: 0, max: 12},

    { key: 'style.labels.timeLabel.show', def: true, type: 'checkbox'},
    { key: 'style.labels.timeLabel.fontSize', def: 12, type: 'number', min: 8, max: 24},
    { key: 'style.labels.timeLabel.color', def: '#1A1A1A', type: 'color'},
    { key: 'style.labels.timeLabel.bold', def: false, type: 'checkbox'},

    { key: 'style.selector.stroke', def: '#3366CC', type: 'color'},
    { key: 'style.selector.strokeWidth', def: 2, type: 'number', min: 0, max: 12},
    { key: 'style.selector.strokeDasharray', def: 2, type: 'number', min: 0, max: 12 },
    { key: 'style.selector.borderRadius', def: 2, type: 'number', min: 0, max: 12},
    { key: 'style.title.textAlign', def: 'left', type: 'select', options: ['left', 'center', 'right']},
    { key: 'style.title.text', def: 'Lorem ipsum dolor sic amet', type: 'text'},
    { key: 'style.title.color', def: '#1A1A1A', type: 'color'},
    { key: 'style.title.fontSize', def: 16, type: 'number', min: 8, max: 24},
    { key: 'style.title.bold', def: false, type: 'checkbox'},
    { key: 'style.title.margin', def: '0 0 6px 0', type: 'text'},
    { key: 'style.title.subtitle.color', def: '#A1A1A1', type: 'color'},
    { key: 'style.title.subtitle.text', def: 'Lorem ipsum dolor sic amet', type: 'text'},
    { key: 'style.title.subtitle.fontSize', def: 12, type: 'number', min: 8, max: 24},
    { key: 'style.title.subtitle.bold', def: false, type: 'checkbox'}
])

const themeOptions = ref([
    "",
    "dark",
    "hack",
    "zen",
    "concrete",
    "default",
    "celebration",
    "celebrationNight"
])

const currentTheme = ref(themeOptions.value[1]);

const configTheme = computed(() => ({ theme: currentTheme.value }));

const config = computed(() => {
    const c = convertArrayToObject(model.value);
    return {
        ...c,
        events: {
            datapointEnter: ({ datapoint, seriesIndex }) => {
                console.log('enter event', { datapoint, seriesIndex })
            },
            datapointLeave: ({ datapoint, seriesIndex }) => {
                console.log('leave event', { datapoint, seriesIndex })
            },
            datapointClick: ({ datapoint, seriesIndex }) => {
                console.log('click event', { datapoint, seriesIndex })
            },
        },
        style: {
            ...c.style,
            labels: {
                ...c.style.labels,
                value: {
                    ...c.style.labels.value,
                    formatter: ({value, config}) => {
                        // console.log(config)
                        return `f - ${value}`
                    }
                }
            }
        },
        theme: currentTheme.value
    }
})

const step = ref(0)

function selectDatapoint(datapoint) {
    // console.log({ datapoint })
}

</script>

<template>
    <div style="margin: 12px 0; color: white">
        Theme:
        <select v-model="currentTheme" @change="step += 1">
            <option v-for="opt in themeOptions">{{ opt }}</option>
        </select>
    </div>

    <button @click="toggleProps">TOGGLE PROPS: {{ isPropsToggled }}</button>
    <button @click="alterDataset">ALTER DATASET</button>

    <Box comp="VueUiSparkHistogram" :dataset="dataset" :config="config">
        <template #title>VueUiSparkHistogram</template>

        <template #respnsive>
            <div style="width: 600px; height: 600px; resize: both; overflow: auto; background: white">
                <LocalVueUiSparkHistogram :dataset="dataset" :config="{ ...config, responsive: true }"/>
            </div>
        </template>

        <template #theme>
            <LocalVueUiSparkHistogram :dataset="dataset" :config="configTheme" />
        </template>

        <template #local>
            <LocalVueUiSparkHistogram :dataset="isPropsToggled ? alternateDataset : dataset" :config="isPropsToggled ? alternateConfig : config" :key="`local_${step}`" @selectDatapoint="selectDatapoint">
                <!-- <template #chart-background>
                    <div style="width: 100%; height: 100%; background: radial-gradient(at top left, red, white)"/>
                </template> -->
                <template #source>
                    <div style="width:100%;font-size:10px;text-align:left">
                        SOURCE: Lorem ipsum dolor sit, amet consectetur adipisicing elit. Tenetur, molestiae perspiciatis nam quae libero, deserunt in aperiam unde officia sint saepe laboriosam ducimus aspernatur labore! Sapiente aspernatur corrupti quis ad.
                    </div>
                </template>
            </LocalVueUiSparkHistogram>
        </template>

        <template #VDUI-local>
            <LocalVueDataUi component="VueUiSparkHistogram" :dataset="isPropsToggled ? alternateDataset : dataset" :config="isPropsToggled ? alternateConfig : config" :key="`VDUI-lodal_${step}`" @selectDatapoint="selectDatapoint">
            </LocalVueDataUi>
        </template>

        <template #build>
            <VueUiSparkHistogram :dataset="isPropsToggled ? alternateDataset : dataset" :config="isPropsToggled ? alternateConfig : config" :key="`build_${step}`" @selectDatapoint="selectDatapoint">
            </VueUiSparkHistogram>
        </template>

        <template #build-treesh>
            <VueUiSparkHistogramTreeshaken :dataset="isPropsToggled ? alternateDataset : dataset" :config="isPropsToggled ? alternateConfig : config" :key="`build_${step}`" @selectDatapoint="selectDatapoint">
            </VueUiSparkHistogramTreeshaken>
        </template>

        <template #VDUI-build>
            <VueDataUi component="VueUiSparkHistogram" :dataset="isPropsToggled ? alternateDataset : dataset" :config="isPropsToggled ? alternateConfig : config" :key="`VDUI-build_${step}`" @selectDatapoint="selectDatapoint">
            </VueDataUi>
        </template>

        <template #knobs>
            <ConfigKnobs :model="model" @change="step += 1"/>
        </template>
    </Box>
</template>